package com.chongbangbang.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.validation.annotation.Validated;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;


/**
 * @author TP
 * @version 1.0
 * @date 2021/11/20 13:18
 */

@Configuration
@EnableOpenApi // swagger，3.0.0(/swagger-ui/index.html访问)
public class SwaggerConfig {

    @Value("${swagger.enabled}")
    private Boolean enabled;

    @Bean
    public Docket blog(Environment environment) {


        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(myApiInfo())
                // API 文档分组
                .groupName("blog")
                // false: 关闭swagger. 默认为true
                .enable(enabled)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.chongbangbang.controller.blog"))
                .paths(PathSelectors.ant("/chong/blog/**"))
                .build();
    }

    @Bean
    public Docket pet(Environment environment) {

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(myApiInfo())
                .groupName("pet")
                .enable(enabled)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.chongbangbang.controller.pet"))
                .paths(PathSelectors.ant("/chong/pet/**"))
                .build();
    }

    @Bean
    public Docket guidance(Environment environment) {

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(myApiInfo())
                .groupName("guidance")
                .enable(enabled)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.chongbangbang.controller.guidance"))
                .paths(PathSelectors.ant("/chong/guidance/**"))
                .build();
    }

    @Bean
    public Docket record(Environment environment) {

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(myApiInfo())
                .groupName("record")
                .enable(enabled)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.chongbangbang.controller.record"))
                .paths(PathSelectors.ant("/chong/record/**"))
                .build();
    }

    @Bean
    public Docket user(Environment environment) {

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(myApiInfo())
                .groupName("user")
                .enable(enabled)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.chongbangbang.controller.user"))
                .paths(PathSelectors.ant("/chong/user/**"))
                .build();
    }

    @Bean
    public Docket recommend(Environment environment) {

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(myApiInfo())
                .groupName("recommend")
                .enable(enabled)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.chongbangbang.controller.recommend"))
                .paths(PathSelectors.ant("/chong/recommend/**"))
                .build();
    }

    @Bean
    public Docket wx(Environment environment) {

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(myApiInfo())
                .groupName("wx")
                .enable(enabled)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.chongbangbang.controller.wx"))
                .paths(PathSelectors.ant("/chong/wx/**"))
                .build();
    }
    @Bean
    public Docket elseInfo(Environment environment) {

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(myApiInfo())
                .groupName("else")
                .enable(enabled)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.chongbangbang.controller"))
                .paths(PathSelectors.ant("/chong/else/**"))
                .build();
    }

    // 配置swagger的信息（ApiInfo），上方的apiInfo
    private ApiInfo myApiInfo() {

        // 作者信息
        Contact contact = new Contact("chong_bang_bang", "","");

        return new ApiInfo("chong_bang_bang's Swagger documentation",
                "2022四川大学 大创",
                "1.0",
                "",
                contact,
                "" + "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }



}

